package admin

import (
	"last/app/models"
	"last/pkg/database"
)

// Categories 表明
type Categories struct {
	models.BaseModel
	Title string `json:"title"`
	Href  string `json:"href"` 
	models.CommonTimestampsField
}

// Store 新增
func (c *Categories) Store(data interface{}) bool {
	result := database.DB.Model(Categories{}).Create(data)
	return result.RowsAffected == 1
}

// List 列表
func (c *Categories) List(page, pageSize int, where map[string]interface{}) (error, int64, interface{}) {
	var total int64 //总条数
	query := database.DB.Model(Categories{})
	var results []Categories
	if where["title"] != "" {
		query.Where("title = ?", where["title"])
	}
	query.Count(&total)
	return query.
		Order("id DESC").
		Offset((page - 1) * pageSize).
		Limit(pageSize).
		Find(&results).
		Error, total, results
}

// Show 详情
func (c *Categories) Show(id string, cate interface{}) error {
	return database.DB.Model(Categories{}).Where("id=?", id).Find(&cate).Error
}

// Remove 删除
func (c *Categories) Remove(id string, cate interface{}) error {
	return database.DB.Model(Categories{}).Where("id=?", id).Delete(&cate).Error
}

// Edit 更新
func (c *Categories) Edit(id int, data interface{}) error {
	return database.DB.Model(Categories{}).Where("id=?", id).Updates(&data).Error
}
